import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TcpClient {
    public static void main(String[] args) {
        try (Socket socket = new Socket("127.0.0.1", 9090)) {
            // 用于向服务器发送数据的输出流
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            // 用于从服务器读取数据的输入流
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("输入消息发送给服务器（输入 'exit' 退出）：");
            String input;
            while ((input = consoleReader.readLine()) != null) {
                if ("exit".equals(input)) {
                    break;
                }
                if (input.isEmpty()) {
                    System.out.println("输入不能为空，请重新输入。");
                } else {
                    out.println(input);
                    System.out.println("服务器响应：" + in.readLine());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}